package com.xinjiang.electricpower.service.impl;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.system.api.domain.SysRole;
import com.ruoyi.system.api.domain.SysUser;
import com.xinjiang.electricpower.domain.DTO.XjOrderGeneralDTO;
import com.xinjiang.electricpower.domain.vo.AuthInfoVo;
import com.xinjiang.electricpower.mapper.SysOrgMapper;
import com.xinjiang.electricpower.service.IUserAuthService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhanghui
 * @date 2023/2/20 15:17
 * @Description
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserAuthServiceImpl implements IUserAuthService {
    private final TokenService tokenService;
    private final SysOrgMapper sysOrgMapper;
    @Override
    public AuthInfoVo getAuthStr() {
        AuthInfoVo authInfoVo = new AuthInfoVo();
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        if(!ObjectUtils.isEmpty(sysUser)){
            List<String> userNames = new ArrayList<>();
            userNames.add(sysUser.getUserName());
            //根据当前账号获取用户对应营销系统的账号
            String mappingCode = sysOrgMapper.getMappingCodeByUserNameAndIntegsysCode(sysUser.getUserName(), Constants.YXYWYXPT);
            if (StringUtils.isNotEmpty(mappingCode)) {
                log.info("当前用户对应营销系统的账号为：{}", mappingCode);
                userNames.add(mappingCode);
            }
            //根据当前账号获取用户对应闭环系统的账号
            String mappingCode1 = sysOrgMapper.getMappingCodeByUserNameAndIntegsysCode(sysUser.getUserName(), Constants.YXYWYXPT);
            if (StringUtils.isNotEmpty(mappingCode1)) {
                log.info("当前用户对应闭环系统的账号为：{}", mappingCode1);
                userNames.add(mappingCode1);
            }
            List<SysRole> roles = sysUser.getRoles();
            List<Long> longs = new ArrayList<>();
            if(!CollectionUtils.isEmpty(roles)){
                longs = roles.stream().map(SysRole::getRoleId).collect(Collectors.toList());
            }
            if(longs.contains(1L)){ // 管理员
                return authInfoVo;
            }
            if(longs.contains(121L)){ // 所长
                authInfoVo.setOrgNo(sysUser.getOrgNo());
            }
            if((longs.contains(184L) || longs.contains(185L) || longs.contains(186L)) && !longs.contains(121L)){
                authInfoVo.setConsNo(userNames);
            }
            if(CollectionUtils.isEmpty(authInfoVo.getConsNo()) && StringUtils.isEmpty(authInfoVo.getOrgNo())){
                userNames.add("fnsj@");
                authInfoVo.setConsNo(userNames);
            }
        }
        log.info("获取用户数据权限字符串： {}", JSON.toJSONString(authInfoVo));
        return authInfoVo;
    }




    /**
     * 待办相关权限参数
     */
    public AuthInfoVo getAuthByDaiBan() {
        //除了管理员，其他人都只能查看自己的待办
        AuthInfoVo authInfoVo = new AuthInfoVo();
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        if(!org.apache.commons.lang3.ObjectUtils.isEmpty(sysUser)){
            List<String> userNames = new ArrayList<>();
            userNames.add(sysUser.getUserName());
            //根据当前账号获取用户对应营销系统的账号
            String mappingCode = sysOrgMapper.getMappingCodeByUserNameAndIntegsysCode(sysUser.getUserName(), Constants.YXYWYXPT);
            if (StringUtils.isNotEmpty(mappingCode)) {
                log.info("当前用户对应营销系统的账号为：{}", mappingCode);
                userNames.add(mappingCode);
            }
            //根据当前账号获取用户对应闭环系统的账号
            String mappingCode1 = sysOrgMapper.getMappingCodeByUserNameAndIntegsysCode(sysUser.getUserName(), Constants.YXYWYXPT);
            if (StringUtils.isNotEmpty(mappingCode1)) {
                log.info("当前用户对应闭环系统的账号为：{}", mappingCode1);
                userNames.add(mappingCode1);
            }
            List<SysRole> roles = sysUser.getRoles();
            List<Long> longs = new ArrayList<>();
            if(!CollectionUtils.isEmpty(roles)){
                longs = roles.stream().map(SysRole::getRoleId).collect(Collectors.toList());
            }
            // 管理员
            if(longs.contains(1L)){
                return authInfoVo;
            }
            if((longs.contains(184L) || longs.contains(185L) || longs.contains(186L)) || longs.contains(121L)){
                authInfoVo.setConsNo(userNames);
            }
            if(CollectionUtils.isEmpty(authInfoVo.getConsNo()) && StringUtils.isEmpty(authInfoVo.getOrgNo())){
                userNames.add("fnsj@");
                authInfoVo.setConsNo(userNames);
            }
        }
        return authInfoVo;
    }


}
